package com.px.web.controller.business;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.px.business.domain.QnrQuestions;
import com.px.business.domain.QnrQuestionsOptions;
import com.px.business.service.IQnrQuestionsService;
import com.px.common.annotation.Log;
import com.px.common.core.controller.BaseController;
import com.px.common.core.domain.AjaxResult;
import com.px.common.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/system/questionsOptions")
public class QuestionsOptionsController extends BaseController {
    @Autowired
    private IQnrQuestionsService qnrQuestionsService;

    @PutMapping
    public AjaxResult edit(@Validated @RequestBody QnrQuestionsOptions qnrQuestionsOptions) {

        QnrQuestions qnrQuestions = qnrQuestionsService.selectQnrQuestionsByQuestionsId(qnrQuestionsOptions.getQuestionsId());

        List<QnrQuestionsOptions> list = new ArrayList<>();
        JSONArray json = JSON.parseArray(qnrQuestions.getQuestionsOptions());
        if (json.size() > 0) {
            for (int i = 0; i < json.size(); i++) {
                JSONObject job = json.getJSONObject(i);  // 遍历 jsonarray 数组，把每一个对象转成 json 对象
                QnrQuestionsOptions qnrQuestionsOptions1 = new QnrQuestionsOptions();
                qnrQuestionsOptions1.setQuestionsOptionsId(job.get("questionsOptionsId").toString());
                qnrQuestionsOptions1.setQuestionsId(qnrQuestions.getQuestionsId());
                qnrQuestionsOptions1.setQuestionsOptionsDescription(job.get("questionsOptionsDescription").toString());
                qnrQuestionsOptions1.setQuestionsOptionsValue(job.get("questionsOptionsValue").toString());
                list.add(qnrQuestionsOptions1);
            }
        }
        for (QnrQuestionsOptions qnrQuestionsOptions2 : list) {
            if (qnrQuestionsOptions.getQuestionsOptionsDescription().equals(qnrQuestionsOptions2.getQuestionsOptionsDescription())) {
                return AjaxResult.error("新增选项'" + qnrQuestionsOptions.getQuestionsOptionsDescription() + "'失败，选项名称已存在");
            }
            if (qnrQuestionsOptions.getQuestionsOptionsValue().equals(qnrQuestionsOptions2.getQuestionsOptionsValue())) {
                return AjaxResult.error("新增选项'" + qnrQuestionsOptions.getQuestionsOptionsValue() + "'分数失败，该分数已存在");
            }
        }
        UUID uuid = UUID.randomUUID();
        qnrQuestionsOptions.setQuestionsOptionsId(uuid.toString());

        list.add(qnrQuestionsOptions);
        QnrQuestions qnrQuestions1 = new QnrQuestions();
        qnrQuestions1.setQuestionsId(list.get(0).getQuestionsId());
        qnrQuestions1.setQuestionsOptions(JSON.toJSONString(list));
        qnrQuestionsService.updateQnrQuestions(qnrQuestions1);

        return null;
    }


    @PreAuthorize("@ss.hasPermi('evaluation:indicator:edit')")
    @Log(title = "【删除指标值域值】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionsId}")
    public AjaxResult remove(@PathVariable("questionsId") Long questionsId) {
        return null;
        //return toAjax(evaluationIndicatorService.deleteEvaluationRangeById(evaluationRangeId));
    }

}
